<?php

if(!defined('_HTML_MENU_FLOAT_')) {
define('_HTML_MENU_FLOAT_', 1);

function itstock_html_menu_float($links, $href, $title = false) {

  echo '<div class="float_menu">';
  if($title) {
    echo "<label><a href='$href.php'>$title</a></label>\n";
  }
  foreach($links as $page => &$name) {
     $class = '';
    if(isset($_GET['page']) && $_GET['page'] === $page) {
      $class= 'class="selected"';
    }
    printf('<a %s href="%s.php?page=%s">%s</a>', $class, $href, $page, $name);

  }
  echo '</div>';
}

/**
 * Creates a vertical HTML menu.
 *
 * @param array $links Array with the links url and text.
 * @param array $title Array with the title url and text.
 * @return void
 */
function itstock_html_menu_float_extra($links, $title = false) {

  echo "<br><br><div class=\"list-group\">\n";
  if($title) {
    echo "<a class=\"list-group-item active\" href='$title[url]'>$title[text]</a>\n";
  }
  foreach($links as &$link) {
    $class = (isset($link['selected'])?'list-group-item-info':'');
    echo "<a class=\"list-group-item $class\" href='$link[url]'>$link[text]</a>\n";
  }
  echo '</div>';
}

function itstock_html_menu_search(&$info) {

  echo "<h3>".$info['menu_title']."</h3>";
  ?>
  <br>
  <div class="menu">
  <?php 
    foreach($info['menu_items'] as &$item) {
      echo $item.'&nbsp;|&nbsp;';
    }
    itstock_html_search($info['search_page']);
  ?>
  </div>
  <br>
  <?php
  itstock_html_message();
}

/**
 * Creates a vertical HTML menu with the data of a 
 * prepared structure.
 *
 * @param mixed  $struct Structure to search.
 * @param string $text   Database field to get the link text.
 * @param string $value  Database field to get the link value.
 * @param string $name   Name of the HTML GET variable.
 * @param string $page   Destination page.
 * @param array  $title  Title and URI to use.
 * @param array  $get    HTML GET variables to use.
 * @return void
 */
function itstock_html_menu_struct(&$struct, $text, $value, $name, $page, $title = false, $get = array()) {
  $links = array();
  if(itstock_search_all($struct)){
    $selected = false;
    if(isset($get[$name])) {
      $selected = itstock_decode($get[$name]);
      unset($get[$name]);
    }
    $url_search = http_build_query($get);
    foreach($struct['data'] as $i => &$item) {
      $links[$i]['text'] = $item[$text];
      $links[$i]['url'] = $page.'?'.$url_search."&$name=".itstock_encode($item[$value]);
      if($selected && "$item[$value]" === "$selected") {
        $links[$i]['selected'] = true;
      }
    }
  }
  itstock_html_menu_float_extra($links, $title);
}

}
?>
